package com.zqwm.fly.http;

import com.zqwm.fly.service.RecipeService;
import com.zqwm.fly.types.PageModel;
import com.zqwm.fly.types.Result;
import com.zqwm.fly.types.ResultType;
import com.zqwm.fly.utils.DataValidator;
import com.zqwm.fly.utils.DateUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜谱数据
 * Created by songyifei on 2018-05-28.
 */

@RestController
@RequestMapping(value = "/recipe")
public class recipeController
{
    @Autowired
    RecipeService recipeService;
    //获取菜谱列表数据

    @CrossOrigin
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public String getRecipeList(@RequestBody Map<String, Object> payload)
    {
        Result result = DataValidator.validateMapData(payload, "pageIndex", "pageSize");
        if (result.getCode() == ResultType.SUCCESS.getValue())
        {
            List<String> tags = new ArrayList<String>();
            //-------智能算法------是否开启智能查询
            if("true".equals(payload.get("flag").toString()))
            {
                //获取季节
                String season = DateUtil.getSeason();
                tags.add(season);
                //通过时间
                String currentTime =DateUtil.getDateSx();
                switch (currentTime)
                {
                    case "早上":tags.add("早点");tags.add("半小时");break;
                    case "上午":tags.add("早餐");break;
                    case "中午":tags.add("午餐");break;
                    case "下午":tags.add("晚餐");break;
                    case "晚上":tags.add("粥");break;
                }
                //通过地址来得到菜系的区域
                tags.add(payload.get("tags").toString());
                payload.put("tags",tags);
            }else {payload.remove("tags");}
            //移除开关
            payload.remove("flag");

            PageModel<Object> page = recipeService.findPageList(payload);

            JSONArray jsonArray = new JSONArray(page.getDatas());
            result.addField("totalPage",page.TotalPages());
            result.addField(jsonArray);
        }

        return result.toString();
    }

    //关键字检索菜谱数据

    //菜谱详细信息
    @CrossOrigin
    @RequestMapping(value = "/getRecipeInfo", method = RequestMethod.POST)
    @ResponseBody
    public String getRecipeInfo(@RequestBody Map<String, Object> payload)
    {
        Result result = DataValidator.validateMapData(payload, "id");
        if (result.getCode() == ResultType.SUCCESS.getValue())
        {
            Object json = recipeService.findById(payload);
            System.out.println(json.getClass());
            LinkedHashMap<String,Object>linkedHashMap = (LinkedHashMap<String, Object>) json;
            JSONObject jsonObject = new JSONObject(linkedHashMap);
            //佐料
            String[]str = jsonObject.getString("burden").split(";");
            JSONArray jsonArray = new JSONArray();
            for(int i = 0; i<str.length;i++)
            {
                String[]item = str[i].split(",");
                jsonArray.put(item);
            }
            jsonObject.put("burden",jsonArray);
            //材料
            String[]ingredients = jsonObject.getString("ingredients").split(";");
            JSONArray ingredientsAr = new JSONArray();
            for(int j = 0; j<ingredients.length;j++)
            {
                String[]items = ingredients[j].split(",");
                ingredientsAr.put(items);
            }
            jsonObject.put("ingredients",ingredientsAr);
            //标签
            jsonObject.put("tags",jsonObject.getString("tags").split(";"));

            result.addField(jsonObject);
        }

        return result.toString();
    }

}
